home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 15 code / Symmetry & Tiles / Tiler Code / TileInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-27  |  5.6 KB  |  207 lines  |  [TEXT/KAHL]

  1. #include     <graphics libraries.h>
  2. #include     <graphics toolbox.h>
  3. #include     <offscreen library.h>
  4. #include     <qd library.h>
  5. #include     <graphics debugging.h>
  6. #include    "TileProtos.h"
  7. #include    "TileConstants.h"
  8.  
  9. #define    kGiveMeValidation    false    /* set to TRUE, for run-time validation  */
  10. #define    kDebugging            true   /* set to TRUE, for error & notice posting. This 
  11.                                     functionality is only available with the "debugging"
  12.                                     Secret Graphics init. */
  13.  
  14.  
  15.  
  16. // Globals only needed in this file
  17. WindowRecord        gWindRecord;
  18. Rect                 gWindowQDRect = {50, 50, 306, 306};
  19.  
  20.  
  21. // Other app globals
  22. extern MenuHandle    gAppMenuHandle;
  23. extern WindowPtr    gWindPtr;
  24. extern gxGraphicsClient     gGraphicsClient;
  25.  
  26. //---------------------------------------
  27. //    Tile globals
  28. extern gxPoint        gOrigin;
  29. extern gxColorSet    gColors;
  30. extern offscreen    gOffScreen;
  31. extern gxShape        gTileShape,    // The contours that fill the unit cell (transforms of gDragger.snake1)
  32.                     gGridShape,    // A rectangle filled with the pattern
  33.                     gBGShape;    // A rectangle filled with the background color
  34.                 
  35. extern dragger        gDragger;    // A record containing manipulable contour(s)
  36. extern gxPatternRecord gPattern;
  37.  
  38. extern Boolean            gContRedraw, gShowVectors; // State Booleans
  39. extern long                gSymmetry;        // the currently chosen symmetry group
  40. extern gxViewPort        gViewPort;
  41.  
  42. // Proto for Gestalt function to check for GX
  43. Boolean QuickDrawGXInstalled(void);
  44.  
  45. Boolean TileInit(void)
  46. {
  47.     unsigned long    seed;
  48.     MenuHandle        mhndl;
  49.     
  50.     // Offscreen stuff
  51.     CTabHandle    standardClut;
  52.     gxBitmap    offMap;
  53.     gxShape        offBits;
  54.     
  55.     // Check for GX
  56.     if(QuickDrawGXInstalled() == false)
  57.     {
  58.         SysBeep(10);
  59.         ExitToShell();
  60.     }
  61.     
  62.     /**     
  63.         Define the graphics heap size. If you do not make this call, graphics will
  64.         create this heap automatically. How? It will create a heap which is a % of your
  65.         application's ideal memory foot print. this call allows you to explicity define 
  66.         the ammount of memory used by the graphics system for it's graphics objects heap.
  67.     **/
  68.     gGraphicsClient = GXNewGraphicsClient(nil, 0L, 0L);
  69.     
  70.     /** 
  71.         Set kDebugging to TRUE, if you want to have graphics library errors & notices posted.  This
  72.         functionality will only work with the "debugging" Secret Graphics init.  If this
  73.         init is not installed, these functions will not work.
  74.     **/
  75.     if (kDebugging) {
  76.         SetGraphicsLibraryErrors ();
  77.         SetGraphicsLibraryNotices();    
  78.     }
  79.  
  80.     /** 
  81.         Set  "kgiveMeValidation" to TRUE, if you want run-time checking. The drawing will get very 
  82.         SLOW due to all of the internal checking. You can set the validation to a higher level,
  83.         see Chapter 3 for details. 
  84.     **/
  85.     if ( kGiveMeValidation ) GXSetValidation(gxPublicValidation); 
  86.  
  87.     /** initialize the new graphics environment **/
  88.     GXEnterGraphics();
  89.  
  90.     // Init the common color library
  91.     InitCommonColors();
  92.        
  93.        // Create the window.
  94.     gWindPtr = NewWindow(        &gWindRecord, 
  95.                                 &gWindowQDRect, 
  96.                                 "\pDrag Colored Shapes",
  97.                                 true, 
  98.                                 documentProc,
  99.                                   (WindowPtr)(-1), 
  100.                                   true,     /* has a close box */
  101.                                   0L    );
  102.     SetPort(gWindPtr);
  103.     ClipRect(&gWindPtr->portRect);
  104.     EraseRect(&gWindPtr->portRect);
  105.     
  106.     gViewPort = GXNewWindowViewPort(gWindPtr);
  107.     GXIgnoreGraphicsNotice(transform_already_set);
  108.     SetDefaultViewPort(gViewPort);
  109.     GXPopGraphicsNotice();
  110.     
  111.     // Reset random number generator
  112.     GetDateTime(&seed);
  113.     qd.randSeed = seed;
  114.     
  115.     /* Set up a color space with the default 8-bit colors for our offscreen */
  116.     standardClut = GetCTable(72);
  117.     gColors = CTableToColorSet( standardClut );
  118.     DisposCTable(standardClut);
  119.  
  120.     /* Set up the off-screen bitmap */
  121.     offMap.image = nil;
  122.     offMap.width = gWindPtr->portRect.right - gWindPtr->portRect.left;
  123.     offMap.height = gWindPtr->portRect.bottom - gWindPtr->portRect.top;
  124.     offMap.rowBytes = 0;
  125.     offMap.pixelSize = 8;
  126.     offMap.space = gxIndexedSpace;
  127.     offMap.set = gColors;
  128.     offMap.profile = nil;
  129.     offBits = GXNewBitmap(&offMap, &gOrigin);
  130.     CreateOffscreen(&gOffScreen, offBits);
  131.     GXDisposeShape(offBits);
  132.     
  133.     // set up background shape with the right color
  134.     gBGShape = GetWindowBoundsShape(gWindPtr);
  135.     SetShapeCommonColor(gBGShape, kBGColor);
  136.  
  137.     // Set up our patterned shape (DefaultTileAndPattern will actually add the pattern to it)
  138.     gGridShape = GXCopyToShape(nil, gBGShape);
  139.     SetShapeCommonColor(gGridShape, kGridColor);
  140.     
  141.     // Set them both to draw offscreen
  142.     GXSetShapeTransform(gBGShape, gOffScreen.xform);
  143.     GXSetShapeTransform(gGridShape, gOffScreen.xform);
  144.     
  145.     // Make the tile shape
  146.     gTileShape = GXNewShape(gxPolygonType);
  147.     GXSetShapeFill(gTileShape, gxOpenFrameFill);
  148.     
  149.     // Set up the dragger. It has one open polygon
  150.     gDragger.snake1 = GXNewShape(gxPolygonType);
  151.     GXSetShapeFill(gDragger.snake1, gxOpenFrameFill);
  152.     SetShapeCommonColor(gDragger.snake1, kDragColor);
  153.     GXSetShapePen(gDragger.snake1, ff(3));
  154.     
  155.     // Set up the initial pattern
  156.     gPattern.attributes = gxNoAttributes;
  157.     gPattern.pattern = gTileShape;
  158.     
  159.     // Set up all the initial geometry
  160.     DefaultTileAndPattern();
  161.     
  162.     // Add the "Tile" menu
  163.     mhndl = GetMenu(kTileMenuID);
  164.     if(mhndl == nil)
  165.         return false;
  166.  
  167.     (*mhndl)->menuID = kTileMenuID;
  168.     InsertMenu(mhndl, 0);
  169.     gAppMenuHandle = mhndl;
  170.     
  171.     /* Update the menu bar */
  172.     DrawMenuBar();
  173.  
  174.     return(true);
  175. }
  176.  
  177. void    TileQuit(void)
  178. {
  179.     // Tile stuff
  180.     DisposeOffscreen(&gOffScreen);
  181.     GXDisposeColorSet(gColors);
  182.     GXDisposeShape(gTileShape);
  183.     GXDisposeShape(gGridShape);
  184.     GXDisposeShape(gBGShape);
  185.     DisposeDragger();
  186.     DisposeOpShapes();
  187.     
  188.     GXDisposeViewPort(GXGetWindowViewPort(gWindPtr));
  189.     if(gWindPtr != nil)
  190.         CloseWindow(gWindPtr);
  191.     
  192.     DisposeCommonColors();
  193. }
  194.  
  195. Boolean QuickDrawGXInstalled(void)
  196. {
  197.     long    theFeatureInQuestion;
  198.     
  199.     //    Check for GX
  200.     if (Gestalt('grfx', &theFeatureInQuestion) == noErr)
  201.         return(true);
  202.     return(false);
  203. }
  204.  
  205.     
  206.  
  207.